<template>
  <div>
    <el-form inline :model="searchForm">
      <el-form-item>
        <el-select v-model="searchForm.status" clearable placeholder="报损状态">
          <el-option label="已处理" :value="1"/>
          <el-option label="未处理" :value="0"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="daterange"
          type="daterange"
          start-placeholder="开始日期"
          value-format="yyyy-MM-dd"
          :editable="false"
          end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList">查询</el-button>
        <el-button type="primary" @click="showDialog()">新增报损</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border>
      <el-table-column prop="lossId" label="报损编号" align="center" header-align="center"/>
      <el-table-column prop="date" label="日期" align="center" header-align="center"/>
      <el-table-column prop="goodsAmount" label="数量" align="center" header-align="center"/>
      <el-table-column prop="reason" label="原因" align="center" header-align="center"/>
      <el-table-column prop="remark" label="备注" align="center" header-align="center"/>
      <el-table-column prop="goodsId" label="商品编号" align="center" header-align="center"/>
      <el-table-column prop="status" label="状态" align="center" header-align="center">
        <!--<template slot-scope="scope">
          <el-tag v-if="scope.row.status===1">已处理</el-tag>
          <el-tag v-else type="danger">未处理</el-tag>
        </template>-->
        <template slot-scope="scope">
          <el-button title="点击更改状态" v-if="scope.row.status === 0" type="danger" size="small" @click="changeStatus(scope.row.lossId,1)">未处理</el-button>
          <el-button @click="changeStatus(scope.row.lossId,0)" type="success" v-else size="small" title="点击更改状态">已处理</el-button>
        </template>
      </el-table-column>
      <el-table-column label="查看" align="center" header-align="center" fixed="right">
        <template slot-scope="scope">
          <el-button size="small" @click="showImageList(scope.row.lossId)">图片</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" header-align="center" fixed="right">
        <template slot-scope="scope">
          <el-button size="small" @click="showDialog(scope.row.lossId)">修改</el-button>
          <el-button size="small" @click="deleteInfo(scope.row.lossId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="pageIndex" :page-size="pageSize" :total="totalPage"
                   :page-sizes="[5,10,15,20,25]" layout="total,sizes,prev,pager,next,jumper"
                   @current-change="currentChange" @size-change="sizeChange"></el-pagination>
    <LossAddOrUpdate ref="addOrUpdate" @onCloseWin="getDataList"/>
    <picture-view :showpage="true" :url-list="imageList"
                  :on-close="closeViewer" v-if="showViewer"/>
  </div>
</template>

<script>
import LossAddOrUpdate from '@/views/loss/LossAddOrUpdate'
import PictureView from '@/views/goods/PictureView'

export default {
  name: 'Loss',
  components: { PictureView, LossAddOrUpdate },
  data () {
    return {
      searchForm: {
        status: '',
        date: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 5,
      totalPage: 0,
      dataListLoading: false,
      daterange: [],
      showViewer: false,
      imageList: []
    }
  },
  methods: {
    getDataList () {
      this.searchForm.date = this.daterange ? this.daterange.toString() : ''/* 任意格式和string都会变成string */
      this.dataListLoading = this.$loading({ text: '加载表格...' })
      this.$http.get('/loss/list', {
        page: this.pageIndex,
        limit: this.pageSize,
        ...this.searchForm
      }).then(data => {
        this.dataListLoading.close()
        if (data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        }
      })
    },
    currentChange (val) {
      this.pageIndex = val
      this.getDataList()
    },
    sizeChange (val) {
      this.pageSize = val
      this.getDataList()
    },
    showDialog (lossId) {
      this.$refs.addOrUpdate.init(lossId)
    },
    changeStatus (lossId, status) {
      this.$confirm('确定更改状态吗？').then(() => {
        this.$http.post('/loss/status', { lossId, status }).then(data => {
          if (data.code === 0) {
            this.$message.success('更新成功')
            this.getDataList()
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    },
    deleteInfo (lossId) {
      this.$confirm(`确定删除编号${lossId}的报损记录吗？`).then(() => {
        this.dataListLoading = this.$loading({ text: '加载表格...' })
        this.$http.delete(`/loss/delete/${lossId}`).then(data => {
          this.dataListLoading.close()
          if (data.code === 0) {
            this.$message({
              message: '删除成功！',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$alert(data.msg, { type: 'error' }).then(() => {
              this.getDataList()
            })
          }
        })
      })
    },
    closeViewer () {
      this.showViewer = false
    },
    showImageList (lossId) {
      this.showViewer = true

      this.dataListLoading = this.$loading({ text: '加载中...' })
      this.$http.get('/loss/images/list/' + lossId).then(data => {
        if (data.code === 0) {
          this.dataListLoading.close()
          if (data.data.length > 0) {
            this.imageList = data.data.map(item => {
              return {
                img: process.env.VUE_APP_BASE_API + '/img/' + item.imageUrl,
                info: [{ label: '图片名字', value: item.imageUrl }]
              }
            })
          } else {
            this.$message.warning('暂无图片')
          }
        }
      })
    }
  },
  created () {
    this.getDataList()
  }
}
</script>

<style scoped>

</style>
